<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'manreader.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!-- 加入easyui -->
	<link rel="stylesheet" type="text/css" href="<%=basePath%>js/jquery-easyui-1.3.5/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>js/jquery-easyui-1.3.5/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>js/jquery-easyui-1.3.5/demo.css">
    <script type="text/javascript" src="<%=basePath%>js/jquery-easyui-1.3.5/jquery.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>js/jquery-easyui-1.3.5/jquery.easyui.min.js"></script>
  	<script type="text/javascript" src="<%=basePath%>js/jquery-easyui-1.3.5/locale/easyui-lang-zh_CN.js"></script>

<script type="text/javascript">
function newUser(){
	$('#dlg').dialog('open').dialog('setTitle','添加新读者');
	$('#fm').form('clear');
	url = '';
}

function editUser(){
    var row = $('#dg').datagrid('getSelected');
    if (row){
        $('#dlgupdate').dialog('open').dialog('setTitle','Edit User');
        $('#fmupdate').form('load',row);
        url = '<%=basePath%>ReaderUpdateServlet?id='+row.id;
    }
}

function saveUser(res){
	$('#fm').form('submit',{
		url: '<%=basePath%>user/adduser',
		onSubmit: function(){
			return $(this).form('validate');
		},
		success: function(result){
			var result = eval('('+result+')');
			
			
			$('#dlg').dialog('close');		// close the dialog
			$('#dg').datagrid('reload');	// reload the user data
			
			if (result.success){
				$.messager.show({
					title: 'success',
					msg: result.success
				});
			}
			if (result.errorMsg){
				$.messager.show({
					title: 'Error',
					msg: result.errorMsg
				});
			} 
			
		}
	});
}

function updateUser(res){
	$('#fmupdate').form('submit',{
		url: '<%=basePath%>user/updateuser',
		onSubmit: function(){
			return $(this).form('validate');
		},
		success: function(result){
			var result = eval('('+result+')');
			
			
			$('#dlgupdate').dialog('close');		// close the dialog
			$('#dg').datagrid('reload');	// reload the user data
			
			if (result.success){
				$.messager.show({
					title: 'success',
					msg: result.success
				});
			}
			if (result.errorMsg){
				$.messager.show({
					title: 'Error',
					msg: result.errorMsg
				});
			} 
			
		}
	});
}

function destroyUser(){
    var row = $('#dg').datagrid('getSelected');
    if (row){
        $.messager.confirm('删除','确认删除?',function(r){
            if (r){
                $.get('<%=basePath%>user/deleteuser',{id:row.id},function(result){
                    if (result.success){
                        $('#dg').datagrid('reload');    // reload the user data
                        $.messager.show({
        					title: 'success',
        					msg: result.success
        				});
                    } else {
                        $.messager.show({    // show error message
                            title: 'Error',
                            msg: result.errorMsg
                        });
                    }
                },'json');
            }
        });
    }
}

</script>

<table id="dg" title="站点维护" class="easyui-datagrid"
	style="width: auto; height: auto" url="<%=basePath%>user/getuserlist"
	toolbar="#toolbar" pagination="true" rownumbers="true"
	fitColumns="true" singleSelect="true">
	<thead>
		<tr>
			<th field="id" width="50">ID</th>
			<th field="account" width="50">用户名</th>
			<th field="password" width="50">密码</th>
			<th field="role" width="50">权限</th>
			<th field="status" width="50">状态</th>
		</tr>
	</thead>
</table>
<script type="text/javascript">
function formatId(value){
	return value.id;
};

</script>
<div id="toolbar">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">添加</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">编辑</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除</a>
</div>

<div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
		closed="true" buttons="#dlg-buttons">
	<form id="fm" method="post">
		<div class="fitem">
			<label>用户名:</label>
			<input name="account"  class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>密码:</label>
			<input name="password" type="password" class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>权限:</label>
			<select id="role" name="role" class="easyui-combobox" required="true" validType="selectValueRequired['#role']">
				<option value="1" selected="selected" >普通管理员</option>
				<option value="11">超级管理员</option>
			</select>
		</div>
		<div class="fitem">
			<label>用户状态:</label>
			<select id="status" name="status" class="easyui-combobox" required="true" validType="selectValueRequired['#status']">
				<option value="0" selected="selected" >待审核</option>
				<option value="1">审核通过</option>
				<option value="2">冻结</option>
			</select>
		</div>
		
	</form>
</div>
<div id="dlg-buttons">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">Save</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Cancel</a>
</div>

<div id="dlgupdate" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
		closed="true" buttons="#dlgupdate-buttons">
	<form id="fmupdate" method="post">
	<div class="fitem">
			<label>ID:</label>
			<input name="id"  class="easyui-validatebox" readonly="readonly">
		</div>
		<div class="fitem">
			<label>网站者名:</label>
			<input name="account"  class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>密码:</label>
			<input name="password" type="password" class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>权限:</label>
			<select id="role" name="role" class="easyui-combobox" required="true" validType="selectValueRequired['#role']">
				<option value="1">普通管理员</option>
				<option value="11">超级管理员</option>
			</select>
		</div>
		<div class="fitem">
			<label>用户状态:</label>
			<select id="status" name="status" class="easyui-combobox" required="true" validType="selectValueRequired['#status']">
				<option value="0">待审核</option>
				<option value="1">审核通过</option>
				<option value="2">冻结</option>
			</select>
		</div>
	</form>
</div>

<div id="dlgupdate-buttons">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="updateUser()">Save</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlgupdate').dialog('close')">Cancel</a>
</div>



  </body>
</html>
